package com.itheima.controller;

import com.itheima.common.response.ResponseResult;
import com.itheima.common.response.StatusCode;
import com.itheima.exception.BusinessExcetion;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @作者 itcast
 * @创建日期 2020/10/12 17:07
 **/
@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    UserService userService;
    @GetMapping
    public ResponseResult findAll(){
        return ResponseResult.okResult(userService.findAll());
    }

    @PostMapping
    public ResponseResult add(@RequestBody User user){
        if(StringUtils.isEmpty(user.getUserName())){
            throw new BusinessExcetion(ResponseResult.errorResult(StatusCode.PARAM_INVALID));
        }
        if (userService.insert(user)) {
            return ResponseResult.okResult(null);
        } else {
            return ResponseResult.errorResult(StatusCode.USER_ADD_ERROR);
        }
    }
}
